import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;

/**
 * @program: LeetCode
 * @description: LeetCode : 406. 根据身高重建队列
 * @author: WXY
 * @create: 2023-01-23 09:27
 * @Version 1.0
 *
 * 先按照身高H排序，从大到小，身高相同，k小的在前 ，形成一个新的数组，nums
 *
 * 然后根据nums数组中num的num[1] 往新结构中插入，
 *
 **/
public class Num406_reconstructQueue {
    public static int[][] reconstructQueue(int[][] people) {
        Arrays.sort(people, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                if (o1[0] == o2[0]) return o1[1] - o2[1];
                return o2[0] - o1[0];
            }
        });
        int[][] ans = new int[people.length][];
        for (int[] num : people) {
            ans[num[1]] =  num;
        }
        LinkedList<int[]> list = new LinkedList<>();
        for (int[] p : people) {
            list.add(p[1], p);
        }
        return list.toArray(new int[people.length][]);
    }

    public static void main(String[] args) {
        int[][] arr = {{7, 0}, {4, 4}, {7, 1}, {5, 0}, {6, 1}, {5, 2}};
        int[][] ints = reconstructQueue(arr);

    }
}
